﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Users
{
    public class PresenterRegisterNewUserView
    {
        IRegisterBasicView viewRegisterBasic;
        Model.User newUser = new Model.User() { Person = new Model.Person() };
        IRegisterNewUserView view;


        public PresenterRegisterNewUserView(IRegisterNewUserView View)
        {
            view = View;
            newUser = new Model.User() { Person = new Model.Person(), UserId = string.Empty }; 
        }


        public void GoStepTwo()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newUser = view.RegisterBasic.UserToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void RegisterUser()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newUser = view.RegisterBasic.UserToRegister;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerUser.NewUser(newUser);
                view.SystemMessage("El usuario fue registrado exitosamente.");
                view.RestartRegisterUser();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.User mp = new Model.User { Person = new Model.Person() };
                mp.Person.PersonId = view.RegisterBasic.UserToRegister.Person.PersonId;
                mp.Person.Names = view.RegisterBasic.UserToRegister.Person.Names;
                mp.Person.LastNames = view.RegisterBasic.UserToRegister.Person.LastNames;
                mp.Person.Phone = view.RegisterBasic.UserToRegister.Person.Phone;

                ManagerUser.VerifyIfCiAlreadyUsed(mp);
                ManagerUser.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.User mp = new Model.User();
                mp.Login = view.RegisterBasic.UserToRegister.Login;
                mp.Role = view.RegisterBasic.UserToRegister.Role;

                ManagerUser.VerifyDataUser(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newUser = view.RegisterBasic.UserToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {
            if (viewRegisterBasic != null)
                newUser = viewRegisterBasic.UserToRegister;
            viewRegisterBasic = view.RegisterBasic;
            viewRegisterBasic.UserToRegister = newUser;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
